package cn.iocoder.yudao.module.mini.service.comment;

import cn.iocoder.yudao.module.mini.dal.mysql.user.MiniUserMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.mini.controller.admin.comment.vo.*;
import cn.iocoder.yudao.module.mini.dal.dataobject.comment.CommentDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.mini.convert.comment.CommentConvert;
import cn.iocoder.yudao.module.mini.dal.mysql.comment.CommentMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mini.enums.ErrorCodeConstants.*;

/**
 * 评论 Service 实现类
 *
 * @author dzy
 */
@Service
@Validated
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private MiniUserMapper miniUserMapper;

    @Override
    public Long createComment(CommentCreateReqVO createReqVO) {
        // 插入
        CommentDO comment = CommentConvert.INSTANCE.convert(createReqVO);
        commentMapper.insert(comment);
        // 返回
        return comment.getId();
    }

    @Override
    public void updateComment(CommentUpdateReqVO updateReqVO) {
        // 校验存在
        validateCommentExists(updateReqVO.getId());
        // 更新
        CommentDO updateObj = CommentConvert.INSTANCE.convert(updateReqVO);
        commentMapper.updateById(updateObj);
    }

    @Override
    public void deleteComment(Long id) {
        // 校验存在
        validateCommentExists(id);
        // 删除
        commentMapper.deleteById(id);
    }

    private void validateCommentExists(Long id) {
        if (commentMapper.selectById(id) == null) {
            throw exception(COMMENT_NOT_EXISTS);
        }
    }

    @Override
    public CommentDO getComment(Long id) {
        CommentDO commentDO = commentMapper.selectById(id);
        commentDO.setTechnicianName(miniUserMapper.selectById(commentDO.getTechnicianId()).getNickname());
        commentDO.setCustomerName(miniUserMapper.selectById(commentDO.getCustomerId()).getNickname());
        return commentDO;
    }

    @Override
    public List<CommentDO> getCommentList(Collection<Long> ids) {
        return commentMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<CommentDO> getCommentPage(CommentPageReqVO pageReqVO) {
        PageResult<CommentDO> commentDOPageResult = commentMapper.selectPage(pageReqVO);
        commentDOPageResult.getList().forEach(item -> {
            item.setCustomerName(miniUserMapper.selectById(item.getCustomerId()).getNickname());
            item.setTechnicianName(miniUserMapper.selectById(item.getTechnicianId()).getNickname());
        });
        return commentDOPageResult;
    }

    @Override
    public List<CommentDO> getCommentList(CommentExportReqVO exportReqVO) {
        return commentMapper.selectList(exportReqVO);
    }

}
